<?php

/*
Zoo Management Simulation
This file determines the admin page actions by deleting, adding and editing cages
Extra functionality includes locking all doors and setting the maximum cages
*/

include("dbconnect.php");

if ($_REQUEST['submit'] == "Show Setup")
{
	header('Location: setupAdmin.php');
}

if ($_REQUEST['submit'] == "Lock")
{
	$closesql = "UPDATE entry SET isEntryOpen=0 WHERE enclosureId =".$_REQUEST['id'];
	$locksql = "UPDATE enclosure SET isEnclosureLocked=1 WHERE enclosureId =".$_REQUEST['id'];
	$dbh->exec($closesql);
	$dbh->exec($locksql);
	header('Location: index.php');
}

if ($_REQUEST['submit'] == "Unlock")
{
	$locksql = "UPDATE enclosure SET isEnclosureLocked=0 WHERE enclosureId = ".$_REQUEST['id'];
	$dbh->exec($locksql);
	header('Location: index.php');
}

if ($_REQUEST['submit'] == "Toggle Lighting")
{
	foreach ($dbh->query("SELECT isEnclosureLightsOn FROM enclosure WHERE enclosureId=".$_REQUEST['id']) as $row){
		$lighting = $row[0];
	}
	if ($lighting == 1){
		$lightsql = "UPDATE `enclosure` SET `isEnclosureLightsOn`=0 WHERE `enclosureId`=".$_REQUEST['id'];
		$dbh->exec($lightsql);
		header('Location: index.php');
	}
	elseif ($lighting == 0){
		$lightsql = "UPDATE `enclosure` SET `isEnclosureLightsOn`=1 WHERE `enclosureId`=".$_REQUEST['id'];
		$dbh->exec($lightsql);
		header('Location: index.php');
	}
}

if ($_REQUEST['submit'] == "Lock all doors")
{
	$closesql = "UPDATE entry SET isEntryOpen=0 WHERE isEntryOpen = 1";
	$locksql = "UPDATE enclosure SET isEnclosureLocked=1 WHERE isEnclosureLocked=0";
	$dbh->exec($closesql);
	$dbh->exec($locksql);
	header('Location: index.php');
}
	$dbh = null;?>